{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.734464</td>\n",
       "      <td>1.246888</td>\n",
       "      <td>0.921260</td>\n",
       "      <td>-0.893499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.954527</td>\n",
       "      <td>-0.573931</td>\n",
       "      <td>-0.893817</td>\n",
       "      <td>1.563767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.713028</td>\n",
       "      <td>-0.259905</td>\n",
       "      <td>-3.059838</td>\n",
       "      <td>-1.824971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.257634</td>\n",
       "      <td>0.224982</td>\n",
       "      <td>0.510233</td>\n",
       "      <td>1.218742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.536384</td>\n",
       "      <td>2.255618</td>\n",
       "      <td>-1.407619</td>\n",
       "      <td>0.545119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.224132</td>\n",
       "      <td>1.863338</td>\n",
       "      <td>0.333345</td>\n",
       "      <td>-1.985052</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0 -1.734464  1.246888  0.921260 -0.893499\n",
       "1  0.954527 -0.573931 -0.893817  1.563767\n",
       "2  0.713028 -0.259905 -3.059838 -1.824971\n",
       "3  0.257634  0.224982  0.510233  1.218742\n",
       "4  2.536384  2.255618 -1.407619  0.545119\n",
       "5  1.224132  1.863338  0.333345 -1.985052"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([\n",
    "    [-1.734464, 1.246888, 0.921260, -0.893499],\n",
    "    [0.954527, -0.573931, -0.893817, 1.563767],\n",
    "    [0.713028, -0.259905, -3.059838, -1.824971],\n",
    "    [0.257634, 0.224982, 0.510233, 1.218742],\n",
    "    [2.536384, 2.255618, -1.407619, 0.545119],\n",
    "    [1.224132, 1.863338, 0.333345, -1.985052],\n",
    "])\n",
    "pd.DataFrame(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0.954527, -0.573931, -0.893817,  1.563767],\n",
       "       [ 0.713028, -0.259905, -3.059838, -1.824971]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 1.246888,  0.92126 ],\n",
       "       [-0.573931, -0.893817],\n",
       "       [-0.259905, -3.059838],\n",
       "       [ 0.224982,  0.510233],\n",
       "       [ 2.255618, -1.407619],\n",
       "       [ 1.863338,  0.333345]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[:, 1:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.257634, 0.224982],\n",
       "       [2.536384, 2.255618],\n",
       "       [1.224132, 1.863338]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[3:, :2]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 使用布尔值选取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "name = np.array(['bob', 'joe', 'will', 'bob', 'will', 'joe', 'joe'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "data = np.arange(name.shape[0] * 5).reshape(name.shape[0], 5)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array(['bob', 'joe', 'will', 'bob', 'will', 'joe', 'joe'], dtype='<U4'),\n",
       " array([[ 0,  1,  2,  3,  4],\n",
       "        [ 5,  6,  7,  8,  9],\n",
       "        [10, 11, 12, 13, 14],\n",
       "        [15, 16, 17, 18, 19],\n",
       "        [20, 21, 22, 23, 24],\n",
       "        [25, 26, 27, 28, 29],\n",
       "        [30, 31, 32, 33, 34]]))"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name, data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ True, False, False,  True, False, False, False])"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "name == 'bob'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[name == 'bob']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ 0,  1,  2,  3,  4],\n",
       "       [15, 16, 17, 18, 19]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "data[[True, False, False, True, False, False, False]]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 练习4：选出1、2列均大于0的行"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>0</th>\n",
       "      <th>1</th>\n",
       "      <th>2</th>\n",
       "      <th>3</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>-1.734464</td>\n",
       "      <td>1.246888</td>\n",
       "      <td>0.921260</td>\n",
       "      <td>-0.893499</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>0.954527</td>\n",
       "      <td>-0.573931</td>\n",
       "      <td>-0.893817</td>\n",
       "      <td>1.563767</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>0.713028</td>\n",
       "      <td>-0.259905</td>\n",
       "      <td>-3.059838</td>\n",
       "      <td>-1.824971</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>0.257634</td>\n",
       "      <td>0.224982</td>\n",
       "      <td>0.510233</td>\n",
       "      <td>1.218742</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>2.536384</td>\n",
       "      <td>2.255618</td>\n",
       "      <td>-1.407619</td>\n",
       "      <td>0.545119</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>5</th>\n",
       "      <td>1.224132</td>\n",
       "      <td>1.863338</td>\n",
       "      <td>0.333345</td>\n",
       "      <td>-1.985052</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "          0         1         2         3\n",
       "0 -1.734464  1.246888  0.921260 -0.893499\n",
       "1  0.954527 -0.573931 -0.893817  1.563767\n",
       "2  0.713028 -0.259905 -3.059838 -1.824971\n",
       "3  0.257634  0.224982  0.510233  1.218742\n",
       "4  2.536384  2.255618 -1.407619  0.545119\n",
       "5  1.224132  1.863338  0.333345 -1.985052"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr = np.array([\n",
    "    [-1.734464, 1.246888, 0.921260, -0.893499],\n",
    "    [0.954527, -0.573931, -0.893817, 1.563767],\n",
    "    [0.713028, -0.259905, -3.059838, -1.824971],\n",
    "    [0.257634, 0.224982, 0.510233, 1.218742],\n",
    "    [2.536384, 2.255618, -1.407619, 0.545119],\n",
    "    [1.224132, 1.863338, 0.333345, -1.985052],\n",
    "])\n",
    "pd.DataFrame(arr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[ True,  True],\n",
       "       [False, False],\n",
       "       [False, False],\n",
       "       [ True,  True],\n",
       "       [ True, False],\n",
       "       [ True,  True]])"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask = (arr[:, 1:3] > 0)\n",
    "mask"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(array([ True, False, False,  True,  True,  True]),\n",
       " array([ True, False, False,  True, False,  True]),\n",
       " array([ True, False, False,  True, False,  True]))"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "mask[:, 0], mask[:, 1], mask[:, 0] & mask[:, 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[-1.734464,  1.246888,  0.92126 , -0.893499],\n",
       "       [ 0.257634,  0.224982,  0.510233,  1.218742],\n",
       "       [ 1.224132,  1.863338,  0.333345, -1.985052]])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "arr[mask[:, 0] & mask[:, 1]]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
